import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:oktoast/oktoast.dart';
import 'package:practice/dto/online_class_dto.dart';
import 'package:practice/dto/remain_dto.dart';
import 'package:practice/net/server_api.dart';
import 'package:practice/ui/studentout/employment_survey_info_detils_page.dart';
import 'package:practice/ui/studentout/employment_survey_info_page.dart';
import 'package:practice/ui/studentout/employment_survey_page.dart';
import 'package:practice/ui/studentout/employment_survey_select_page.dart';
import 'package:practice/ui/studentout/examination_information_page.dart';
import 'package:practice/ui/studentout/meeting_page.dart';
import 'package:practice/ui/studentout/online_class_info_page.dart';
import 'package:practice/ui/studentout/online_class_page.dart';
import 'package:practice/ui/studentout/resource_sharing_page.dart';
import 'package:practice/ui/studentout/study_list_page.dart';
import 'package:practice/widget/custom_app_bar.dart';
import 'package:practice/widget/image_widget.dart';

import '../../toast_utils.dart';

class StudentInPage extends StatefulWidget {
  const StudentInPage({Key? key}) : super(key: key);

  @override
  _StudentInPageState createState() => _StudentInPageState();
}

class _StudentInPageState extends State<StudentInPage> {
  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
        future: getRemainData(),
        builder: (context, snp) {
          if (snp.connectionState == ConnectionState.done) {
            getVideoData();
            return Scaffold(
                appBar: AppBar(
                  leading: Text(''),
                  title: Text(
                    '首页',
                    style: TextStyle(color: Colors.black),
                  ),
                  centerTitle: true,
                  elevation: 0,
                ),
                body: Container(
                  color: Color.fromRGBO(245, 247, 250, 1),
                  child: ListView(
                    children: [
                      ///banner
                      Container(
                        child: Column(
                          children: [
                            Container(
                              child: ImageAsset('assets/images/banner .png'),
                              margin: EdgeInsets.only(
                                  left: 7.5.w, right: 7.5.w, bottom: 0),
                            ),
                            Container(
                              child: ListTile(
                                leading: ImageAsset('assets/images/p.png',
                                    width: 16.w, height: 16.w),
                                title: Text('附近职位更新啦，快来看看吧！'),
                                trailing: Icon(Icons.keyboard_arrow_right),
                              ),
                            )
                          ],
                        ),
                        color: Colors.white,
                      ),
                      //中层
                      Container(
                        color: Colors.white,
                        margin: EdgeInsets.only(top: 10),
                        height: 176.5,
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                          children: [
                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                              children: [
                                meau(
                                    "assets/images/studyrw.png",
                                    "实习任务",
                                    () => Get.to(() => EmploymentSurveyPage())!
                                            .then((value) {
                                          setState(() {});
                                        })),
                                meau("assets/images/gwrz.png", "岗位热招",
                                    () => Get.to(() => ResourceSharingPage())),
                                meau("assets/images/jianli.png", "简历管理",
                                    () => Get.to(() => MeetingPage())),
                                meau(
                                    "assets/images/hd.png",
                                    "活动评价",
                                    () => Get.to(
                                        () => ExaminationInformationPage())),
                              ],
                            ),
                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                              children: [
                                meau(
                                    "assets/images/hdzj.png",
                                    "活动总结",
                                    () => Get.to(() => EmploymentSurveyPage())!
                                            .then((value) {
                                          setState(() {});
                                        })),
                                meau("assets/images/rb.png", "活动日报",
                                    () => Get.to(() => ResourceSharingPage())),
                                meau("assets/images/yueb.png", "活动月报",
                                    () => Get.to(() => MeetingPage())),
                                meau(
                                    "assets/images/more.png",
                                    "更多",
                                    () => Get.to(
                                        () => ExaminationInformationPage())),
                              ],
                            ),
                          ],
                        ),
                      ),

                      ///示教课
                      InkWell(
                        child: Container(
                          margin: EdgeInsets.only(top: 10),
                          color: Colors.white,
                          child: Column(
                            children: [
                              Row(
                                children: <Widget>[
                                  InkWell(
                                    onTap: () =>
                                        Get.to(() => OnlineClassPage()),
                                    child: carContainer(
                                        Color.fromRGBO(228, 237, 253, 1),
                                        '示教课',
                                        Color.fromRGBO(27, 86, 133, 1),
                                        'Teaching',
                                        'assets/images/sl.png'),
                                  ),
                                  InkWell(
                                    onTap: () => Get.to(() => StudyListPage()),
                                    child: carContainer(
                                        Color.fromRGBO(252, 244, 228, 1),
                                        '技能竞赛',
                                        Color.fromRGBO(127, 81, 29, 1),
                                        'competition',
                                        'assets/images/mj.png'),
                                  ),
                                ],
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceEvenly,
                              ),
                            ],
                          ),
                        ),
                      ),

                      ///任务
                      remainDto!.rows!.length > 0
                          ? InkWell(
                              child: Container(
                                color: Colors.white,
                                padding: EdgeInsets.all(6),
                                child: Container(
                                  margin: EdgeInsets.only(
                                      top: 20, left: 20, right: 20),
                                  height: 130,
                                  width: 366.w,
                                  decoration: new BoxDecoration(
                                    boxShadow: [
                                      BoxShadow(
                                          color: Colors.grey, blurRadius: 6.0.w)
                                    ],
                                    color: Colors.white,
                                    borderRadius:
                                        new BorderRadius.circular((10.0.w)),
                                  ),
                                  child: Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceBetween,
                                    children: [
                                      Container(
                                        margin: EdgeInsets.only(left: 12.w),
                                        child: InkWell(
                                          onTap: () {
                                            if (remainDto!.rows![0].type == 2) {
                                              if (remainDto!
                                                      .rows![0].researchType ==
                                                  1) {
                                                Get.to(() =>
                                                        EmploymentSurveyInfoPage(
                                                          recordId: remainDto!
                                                              .rows![0].busId,
                                                        ))!
                                                    .then((value) {
                                                  setState(() {});
                                                });
                                              } else {
                                                Get.to(() =>
                                                        EmploymentSurveySelectPage(
                                                          tempId: remainDto!
                                                              .rows![0].tempId,
                                                          rId: remainDto!
                                                              .rows![0].busId,
                                                          type: 1,
                                                        ))!
                                                    .then((value) {
                                                  setState(() {});
                                                });
                                              }
                                            } else {}
                                          },
                                          child: Column(
                                            mainAxisAlignment:
                                                MainAxisAlignment.spaceEvenly,
                                            crossAxisAlignment:
                                                CrossAxisAlignment.start,
                                            children: [
                                              Text(
                                                remainDto!.rows!.length > 0
                                                    ? remainDto!.rows![0].title
                                                        .toString()
                                                    : "",
                                                style: TextStyle(
                                                    fontSize: 16,
                                                    fontWeight:
                                                        FontWeight.bold),
                                              ),
                                              rwRow(
                                                  "assets/images/rw.png",
                                                  remainDto!.rows!.length > 0
                                                      ? remainDto!
                                                          .rows![0].teacherName
                                                      : ''),
                                              rwRow("assets/images/rws.png",
                                                  '待办任务'),
                                            ],
                                          ),
                                        ),
                                      ),
                                      InkWell(
                                        onTap: () => Get.to(
                                                () => EmploymentSurveyPage())!
                                            .then((value) {
                                          setState(() {});
                                        }),
                                        child: Container(
                                          margin: EdgeInsets.only(right: 14.w),
                                          child: ImageAsset(
                                              'assets/images/right.png',
                                              width: 14.w,
                                              height: 14.w),
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                              ),
                            )
                          : Container(),

                      ///课堂标题
                      Container(
                        color: Colors.white,
                        child: ListTile(
                          onTap: () => Get.to(() => OnlineClassPage()),
                          leading: Text(
                            '在线课堂',
                            style: TextStyle(
                                fontWeight: FontWeight.bold, fontSize: 16),
                          ),
                          title: Container(
                            alignment: Alignment.centerRight,
                            child: Text(
                              "查看更多",
                              style: TextStyle(
                                  fontSize: 12,
                                  color: Color.fromRGBO(160, 161, 163, 1)),
                            ),
                          ),
                          trailing: Icon(
                            Icons.keyboard_arrow_right,
                            size: 14,
                          ),
                        ),
                      ),

                      ///课堂详情
                      Container(
                        color: Colors.white,
                        padding: EdgeInsets.only(left: 15.w, right: 15.w),
                        child: Column(children: cla),
                      ),
                    ],
                  ),
                ));
          } else {
            return Container(
              color: Colors.white,
              child: CupertinoActivityIndicator(),
            );
          }
        });
  }

  RemainDto? remainDto;
  @override
  void initState() {
    super.initState();

    getRemainData();
  }

  OnlineClassDto? onlineClassDto;

  getRemainData() async {
    remainDto = await ServerApi.remain();
    onlineClassDto = await ServerApi.onlineClass();
  }

  ///课堂详情
  ///
  List<Widget> cla = [];
  getVideoData() {
    cla = [];
    onlineClassDto!.rows!.forEach((element) {
      cla.add(
        InkWell(
          onTap: () {
            Get.to(() => OnlineClassInfoPage(
                  onlineCourseId: element.id,
                ));
          },
          child: Container(
            margin: EdgeInsets.only(bottom: 10),
            child: Row(
              children: <Widget>[
                Stack(
                  children: [
                    ClipRRect(
                      borderRadius: BorderRadius.circular(6),

                      ///切换背景图
                      child: Container(
                        width: 114.w,
                        height: 85,
                        color: Colors.green,
                        child: Image.network(
                          element.cover.toString(),
                          fit: BoxFit.cover,
                          errorBuilder: (
                            BuildContext context,
                            Object error,
                            StackTrace? stackTrace,
                          ) {
                            return Container(
                              width: 114.w,
                              height: 85,
                              color: Colors.green,
                            );
                          },
                        ),
                      ),
                    ),
                    Container(
                      width: 114.w,
                      height: 85,
                      alignment: Alignment.center,
                      decoration:
                          BoxDecoration(borderRadius: BorderRadius.circular(6)),
                      child: ImageAsset('assets/images/play.png',
                          width: 25.w, height: 25.w),
                    ),
                  ],
                ),
                SizedBox(
                  width: 14.5.w,
                ),

                ///右侧
                Expanded(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      ///标题
                      Container(
                        child: Text(
                          element.courseName ?? '',
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                          style: TextStyle(
                              fontWeight: FontWeight.bold, fontSize: 14),
                        ),
                      ),

                      ///二级标题
                      ///
                      Container(
                        child: Text(
                          element.summary ?? '',
                          overflow: TextOverflow.ellipsis,
                          style: TextStyle(
                              fontSize: 14,
                              color: Color.fromRGBO(160, 161, 163, 1)),
                        ),
                      ),

                      ///头像等
                      ///
                      Container(
                        margin: EdgeInsets.only(top: 13),
                        child: Row(
                          children: [
                            ///头像
                            ClipOval(
                              child: Container(
                                color: Colors.green,
                                width: 20.w,
                                height: 20.w,
                                child: ImageAsset('assets/images/Clip.png',
                                    width: 20.w, height: 20.w),
                              ),
                            ),
                            SizedBox(
                              width: 9.5.w,
                            ),

                            ///昵称
                            ///
                            Text(
                              '大咖讲',
                              style: TextStyle(
                                  fontSize: 11,
                                  color: Color.fromRGBO(151, 152, 154, 1)),
                            ),

                            Expanded(
                              flex: 2,
                              child: Container(
                                  alignment: Alignment.centerRight,
                                  child: ImageAsset(
                                      'assets/images/playcount.png',
                                      width: 10.w,
                                      height: 10.w)),
                            ),
                            Expanded(
                              flex: 1,
                              child: Container(
                                alignment: Alignment.centerRight,
                                child: Text(
                                  element.buyCount.toString(),
                                  style: TextStyle(
                                      fontSize: 11,
                                      color: Color.fromRGBO(151, 152, 154, 1)),
                                ),
                              ),
                            )
                          ],
                        ),
                      )
                    ],
                  ),
                )
              ],
            ),
          ),
        ),
      );
    });
  }

  Row rwRow(path, other) {
    return Row(
      children: [
        ImageAsset(path, width: 12.w, height: 12.w),
        SizedBox(
          width: 8.w,
        ),
        Text(
          other,
          style:
              TextStyle(fontSize: 13, color: Color.fromRGBO(102, 102, 102, 1)),
        ),
      ],
    );
  }

  Container carContainer(
      Color col, title, Color titleCol, subTitle, imagePath) {
    return Container(
      margin: EdgeInsets.only(top: 20),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: [
          Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
              Text(
                title,
                style: TextStyle(
                    fontSize: 16, color: titleCol, fontWeight: FontWeight.bold),
              ),
              Text(subTitle,
                  style: TextStyle(
                      fontSize: 13,
                      color: titleCol,
                      fontWeight: FontWeight.w300)),
            ],
          ),
          ImageAsset(imagePath, width: 44.w, height: 44.w)
        ],
      ),
      width: 165.w,
      height: 76,
      decoration:
          BoxDecoration(color: col, borderRadius: BorderRadius.circular(6)),
    );
  }

  InkWell meau(path, data, onClick) {
    return InkWell(
      onTap: onClick,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          ImageAsset(path, width: 24.w, height: 24.w),
          SizedBox(
            height: 6,
          ),
          Text(
            data,
            style: TextStyle(fontSize: 12),
          )
        ],
      ),
    );
  }
}
